web/src/pages/[category].astro
157 lines · 3623 bytes
1---
2
3import Layout from '@layouts/Layout.astro';
4import SectionList from '@components/things/SectionList.astro';
5import Main from '@components/scafold/MainCard.astro';
6
7import { fetchData, slugify } from '@utils/fetch-data';
8
9import type { Section } from '../types/Service';
10import Button from '@components/form/Button.astro';
11
12interface Props {
13 title: string;
14 sections: Section[];
15}
16
17const { title, sections } = Astro.props;
18
19const categories = (await fetchData()).categories;
20
21export async function getStaticPaths() {
22 const pages = await fetchData().then((data) => {
23 return data.categories.map((category) => {
24 return {
25 category: slugify(category.name),
26 title: category.name,
27 sections: category.sections,
28 };
29 });
30 });
31
32 return pages.map(({ category, title, sections }) => {
33 return {
34 params: { category },
35 props: { title, sections },
36 };
37 });
38}
39
40const makeCarasolData = () => {
41 return {
42 "@context": "https://schema.org",
43 "@type": "ItemList",
44 "itemListElement": [
45 sections.map((section, index) => ({
46 "@type": "ListItem",
47 "position": index + 1,
48 "url": `https://awesomeprivacy.com/${slugify(title)}/${slugify(section.name)}`,
49 "item": {
50 "@type": "Service",
51 "name": section.name,
52 "url": `https://awesomeprivacy.com/${slugify(title)}/${slugify(section.name)}`,
53 }
54 })),
55 ]
56 }
57};
58
59const makeBreadcrumbs = () => {
60 return [
61 { name: 'Home', item: '/' },
62 { name: title, item: slugify(title) },
63 ];
64};
65
66
67const makePaginationLinks = () => {
68 const index = categories.findIndex(category => category.name === title);
69 const previousCategory = index > 0 ? categories[index - 1].name : null;
70 const nextCategory = index < categories.length - 1 ? categories[index + 1].name : null;
71 return { previous: previousCategory, next: nextCategory };
72};
73
74const { previous, next } = makePaginationLinks();
75
76---
77
78<Layout title={`${title} | Awesome Privacy`} breadcrumbs={makeBreadcrumbs()} customSchemaJson={makeCarasolData()}>
79 <Main>
80 <div class="breadcrumbs">
81 <span>
82 <a href="/">Awesome Privacy</a>
83 ➔ <a href={`/${slugify(title)}`}>{title}</a>
84 </span>
85 </div>
86 <SectionList title={title} sections={sections} bigTitle={true} />
87 Or, <a href={`/all#${slugify(title)}`}>Browse All {title}</a>
88 <div class="pagination-navigation">
89 { previous ? (
90 <Button url={`/${slugify(previous)}`}>
91 <span>← Previous</span>
92 <p>{previous}</p>
93 </Button>
94 ) : <p class="nothing"></p>}
95 { next && (
96 <Button url={`/${slugify(next)}`}>
97 <span>Next →</span>
98 <p>{next}</p>
99 </Button>
100 )}
101 </div>
102 </Main>
103</Layout>
104
105<style lang="scss">
106.pagination-navigation {
107 display: flex;
108 justify-content: space-between;
109 align-items: center;
110 padding: 1rem 0;
111 :global(.button) {
112 min-width: 120px;
113 width: fit-content;
114 padding: 0.25rem 1rem;
115 text-align: right;
116 &:first-child { text-align: left; }
117 p {
118 margin: 0;
119 font-weight: normal;
120 font-size: 1rem;
121 }
122 span {
123 font-size: 0.8rem;
124 }
125
126 }
127 .nothing {
128 width: 120px;
129 }
130 .go-to-category {
131 color: var(--foreground);
132 font-size: 0.8rem;
133 opacity: 0.5;
134 }
135}
136
137
138.breadcrumbs {
139 opacity: 0.5;
140 font-size: 0.8rem;
141 position: absolute;
142 right: 1rem;
143 top: 1rem;
144 a {
145 color: var(--foreground);
146 transition: all 0.15s ease-in-out;
147
148 &:hover {
149 color: var(--accent);
150 }
151 }
152 @media(max-width: 768px) {
153 display: none;
154 }
155}
156
157</style>